<?php
namespace Application\pform\a_wan;

use Application\model\BasePayModel;

/**
 * @property string $app_order_id 游戏订单号
 * @property string $xx_game_id 渠道子包ID
 * @property string $user_id 平台用户ID
 * @property string $total_fee 实际充值金额：人民币（分）
 * @property string $channel 玩心发行平台ID
 * @property string $server_id 游戏服务器ID
 * @property string $app_role_id 游戏角色ID
 * @property string $role_name 游戏角色名
 * @property string $product_id 商品ID
 * @property string $order_id SDK订单号
 * @property string $is_sandbox 是否为测试订单 1是测试订单，0是正常订单
 * @property string $tp 游戏方透传参数，原样返回。(例如订单号)
 * @property string $time  发送请求的时间，UNIX时间戳
 * @property string $sign
 */
class PayModel extends BasePayModel{

    protected $v = 1;
    protected $auto_set_map = [
        'orderId' => 'app_order_id',
        'cpid' => 'xx_game_id',
        'account' => 'user_id',
        'sid' => 'server_id',
        'amount' => 'total_fee',
        'actorId' => 'app_role_id',
        'isTest' => 'is_sandbox',
        'actorName' => 'role_name',
        'shopId' => 'product_id',
        'ccid' => 'channel',
        'openid' => 'user_id',
    ];

    /**
     * 初始化支付回调参数
     * @return void
     */
    protected function initPayParams()
    {
        $this->amount = round($this->amount / 100, 2);
    }

    /**
     * 计算签名，平台订单验证签名
     * @return string
     */
    protected function sign()
    {
        // 获取参与签名的参数
        $params = array();
        $params['app_order_id'] = $this->app_order_id;
        $params['app_role_id'] = $this->app_role_id;
        $params['is_sandbox'] = $this->is_sandbox;
        $params['xx_game_id'] = $this->xx_game_id;
        $params['order_id'] = $this->order_id;
        $params['product_id'] = $this->product_id;
        $params['server_id'] = $this->server_id;
        $params['total_fee'] = $this->total_fee;
        $params['user_id'] = $this->user_id;
        if($this->v==2){
            $params['timestamp'] = $this->timestamp;
            $params['channel_id'] = $this->channel_id;
        }else{
            $params['time'] = $this->time;
            $params['channel'] = $this->channel;
        }


        if (isset($this->tp)) {
            // 存在tp参数时需加入签名
            $params['tp'] = $this->tp;
        }

        if($this->v==1){
            // 字典排序
            ksort($params);
            // 获取签名串
            $signStr = "";
            foreach ($params as $key=>$param) {
                $signStr = $signStr."&{$key}={$param}";
            }
            $signStr = substr($signStr, 1).PAY_SIGN;
            // md5获得签名
            $sign = md5($signStr);
            return $sign;
        }else{
            list($signStr,$sign) = PublicModel::signV22($params,PAY_SIGN,['tp']);
            $this->logfile("v===".$this->v);
            $this->logfile("before==".($signStr ?? ''));
            $this->logfile("$sign==".$sign);
            return $sign;
        }

    }

    public function __errmsg($status, $isdebug = false)
    {
        $code = 0;
        if($status === SUCCESS) {
            $code = 1;
        }

        exit(json_encode([
            'ret' => $code,
            "msg" => $status,
            "content" => []
        ]));
    }

}
